<?php


namespace app\common\controller;


use app\common\helper\cache\Cache;
use app\common\helper\cache\Session;
use app\common\helper\util\ApiDoc;
use app\common\model\CoreSetting;
use think\App;
use think\Controller;
use think\facade\Cookie;
use think\facade\Request;
use think\facade\Response;

class BaseController extends Controller
{
    static $CORS_HEADER ;
    static $cache;
    static $core_set;
    static $session;

    function __construct(App $app = null)
    {
        BEGIN:
        $token = Cookie::get("SESSION_ID");
        if(empty($token)){
            Cookie::set("SESSION_ID",md5(time().rand(1000,9999)));
            goto BEGIN;
        }
        parent::__construct($app);
        self::$cache = Session::init_connection();
        self::$session = Session::__init__($token);
        self::$core_set = CoreSetting::debug();
        $referr = isset($_SERVER['HTTP_REFERER'])? substr( $_SERVER['HTTP_REFERER'],0,-1):"*";
        self::$CORS_HEADER = [
            "Access-Control-Allow-Origin"=>$referr,
            "Access-Control-Allow-Methods"=>"POST, GET, OPTIONS, DELETE",
            "Access-Control-Allow-Credentials"=>"true",
        ];
    }

    public  function result_success($data=[],$error_code = 0,$msg="成功"){
        if(is_string($data)){
            $msg = $data;
        }
        $this->result($data,$error_code,$msg,"json",self::$CORS_HEADER);
    }
    public  function result_error($data=[],$error_code = -1,$msg="异常"){
        if(is_string($data)){
            $msg = $data;
        }
        $this->result($data,$error_code,$msg,"json",self::$CORS_HEADER);
    }


    /**
     * 返回api 文档
     */
    public function doc(){
        dump(ApiDoc::genClass(static::class));
    }
}